package cn.org.seventh.web.aop;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.BeanUtils;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.AbstractUrlBasedView;
import org.springframework.web.servlet.view.InternalResourceView;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.RedirectView;
import org.springframework.web.servlet.view.freemarker.FreeMarkerView;
import org.springframework.web.servlet.view.jasperreports.JasperReportsMultiFormatView;
import org.springframework.web.servlet.view.velocity.VelocityView;
import org.springframework.web.servlet.view.xslt.XsltView;

import cn.org.seventh.web.annotation.View;
import cn.org.seventh.web.annotation.ViewType;
import cn.org.seventh.web.common.CustomLocaleResolver;
import cn.org.seventh.web.common.CustomWebApplicationObjectSupport;

/**
 * AOP通知定义类 这里定义所有AOP通知
 * 
 * @author Seventh
 */
@Aspect
public class AdviceDefinitions extends CustomWebApplicationObjectSupport
		implements Ordered {

	private CustomLocaleResolver localeResolver;

	public CustomLocaleResolver getLocaleResolver() {
		return localeResolver;
	}

	public void setLocaleResolver(CustomLocaleResolver localeResolver) {
		this.localeResolver = localeResolver;
	}

	public int getOrder() {
		return 0;
	}

	/**
	 * 按自定义方式处理视图
	 * 
	 * @param jp
	 * @param view
	 * @param request
	 * @param response
	 * @return
	 */
	@Around("cn.org.seventh.web.aop.PointcutDefinitions.viewSupportPointcut(view,request,response)")
	public Object doCreateView(ProceedingJoinPoint jp, View view,
			HttpServletRequest request, HttpServletResponse response) {
		ModelAndView modelAndView = null;
		try {
			modelAndView = (ModelAndView) jp.proceed();
			if (modelAndView instanceof ModelAndView) {
				// 这种情况不处理交给spring处理
				if (view.type().equals(ViewType.OTHER)) {
					return modelAndView;
				}
				Class type = view.type().getType();
				String viewName = modelAndView.getViewName();
				String path = localeResolver.getViewDir(request);
				if (type.equals(RedirectView.class)
						|| type.equals(InternalResourceView.class)
						|| type.equals(JstlView.class)
						|| type.equals(FreeMarkerView.class)
						|| type.equals(VelocityView.class)
						|| type.equals(XsltView.class)
						|| type.equals(JasperReportsMultiFormatView.class)) {
					AbstractUrlBasedView viewBean = (AbstractUrlBasedView) BeanUtils
							.instantiateClass(type);
					String url = type.equals(RedirectView.class)
							|| type.equals(InternalResourceView.class) ? viewName
							: path + viewName;
					viewBean.setUrl(url);
					viewBean = (AbstractUrlBasedView) getWebApplicationContext()
							.getAutowireCapableBeanFactory().initializeBean(
									viewBean, "" + System.currentTimeMillis());
					modelAndView.setView(viewBean);
					response.setCharacterEncoding("UTF-8");
					response.setContentType("text/html");
					response.setLocale(localeResolver.resolveLocale(request));
					return modelAndView;
				}
				// if (AbstractPdfView.class.isAssignableFrom(type)
				// || AbstractJExcelView.class.isAssignableFrom(type)
				// || AbstractExcelView.class.isAssignableFrom(type)
				// || AbstractBinView.class.isAssignableFrom(type)) {
				// AbstractView viewBean =
				// (AbstractView) getWebApplicationContext()
				// .getAutowireCapableBeanFactory().initializeBean(
				// BeanUtils.instantiateClass(type),
				// "" + System.currentTimeMillis());
				// modelAndView.setView(viewBean);
				// return modelAndView;
				// }
			}
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return modelAndView;
	}
}
